package com.x.uikit.page;

import android.content.Context;
import android.os.Bundle;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;


public class XPage extends FrameLayout implements XLifecycleProvider, XLifecycleListener {
    protected XPageInfo pageInfo;
    protected Callback mCallback;
    protected final XLifecycle mLifecycle = new XLifecycle();
    protected Bundle mArgument;

    public XPage(@NonNull Context context) {
        super(context);
        /**
         * 设置默认背景为白色，如果需要设置不同的背景颜色，可以在子类中修改
         */
        setBackgroundColor(0xffffffff);
        pageInfo = new XPageInfo();
        configPageInfo(pageInfo);
        setFitsSystemWindows(true);
        mLifecycle.addObserver(this);
    }

    public Bundle getArgument() {
        return mArgument;
    }

    public void setArgument(Bundle argument) {
        this.mArgument = argument;
    }

    protected void configPageInfo(@NonNull XPageInfo pageInfo) {

    }

    public XPageInfo getPageInfo() {
        return pageInfo;
    }

    public void setCallback(Callback callback) {
        this.mCallback = callback;
    }


    public boolean onBackPressed() {
        finish();
        return true;
    }

    protected void finish() {
        finish(true);
    }

    protected void finish(boolean animated) {
        XPageManager manager = XPageManager.of(getContext());
        if (manager != null) {
            manager.finish(this, animated);
        }
    }

    protected void finish(IPageAnimator animator) {
        XPageManager manager = XPageManager.of(getContext());
        if (manager != null) {
            manager.finish(this, animator);
        }
    }

    @Override
    public XLifecycle getLifecycle() {
        return mLifecycle;
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onDestroy() {

    }

    public interface Callback {
        void onCloseWindow();
    }
}
